package com.yaozheng.vocationaltraining.fragment.set_up;

import android.widget.EditText;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.set_up.ISuggestView;
import com.yaozheng.vocationaltraining.service.impl.set_up.SuggestServiceImpl;
import com.yaozheng.vocationaltraining.service.set_up.SuggestService;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 意见反馈(建议或意见)
 */
@EFragment(R.layout.fragment_suggest)
public class SuggestFragment extends BaseFragment implements ISuggestView {
    @ViewById
    EditText suggestContentEditView, suggestPhoneEditView;
    @Bean(SuggestServiceImpl.class)
    SuggestService suggestService;
    @ViewById
    TextView headTitleText;

    @AfterViews
    public void initView() {
        headTitleText.setText("建议或意见");
        suggestService.init(this);
    }

    /**
     * 反馈提交
     */
    @Click(R.id.suggestSubmitButton)
    public void settingSubmitClick() {
        String suggestContent = suggestContentEditView.getText().toString();
        String suggestPhone = suggestPhoneEditView.getText().toString();
        if (suggestContent == null || "".equals(suggestContent)) {
            alertMessage("反馈内容不能为空");
            return;
        }
        if (suggestContent.length() > 200) {
            alertMessage("反馈内容不能大于200字符");
            return;
        }
        if (suggestPhone == null || "".equals(suggestPhone)) {
            alertMessage("联系电话不能为空");
            return;
        }
        showOperatingProgressDialog("提交中...");
        suggestService.suggest(suggestContent, suggestPhone);
    }

    @UiThread
    @Override
    public void suggestSuccess(JSONObject jsonObject) {
        cancelOperatingProgressDialog();
        boolean success = TypeUtils.getJsonBoolean(TypeUtils.getJsonObject(jsonObject, "data"), "success", false);
        if (success) {
            alertMessage("提交成功,谢谢您的反馈。");
        } else {
            alertMessage("反馈失败,请联系后台管理员");
        }
    }

    @UiThread
    @Override
    public void suggestError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);
    }
}
